package com.zxy.demo.junit.rule;

import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;

/**
 * ExpectedExceptionTest.
 * 
 * Created by zxybird on 17/5/16.
 */
public class ExpectedExceptionTest {

  @Rule
  public ExpectedException exp = ExpectedException.none();

  @Test
  public void expectException1() {
    exp.expect(IndexOutOfBoundsException.class);
    throw new IndexOutOfBoundsException("Exception method.");
  }

  @Test(expected = IndexOutOfBoundsException.class)
  public void expectException2() {
    throw new IndexOutOfBoundsException("Exception method.");
  }

  @Test
  public void expectMessage() {
    exp.expectMessage("Hello World");
    throw new RuntimeException("Hello World will throw exception.");
  }

  // @Test
  // public void expectCourse() {
  // exp.expectCause(new BaseMatcher<IllegalArgumentException>() {
  //
  // public boolean matches(Object item) {
  // return item instanceof IllegalArgumentException;
  // }
  //
  // public void describeTo(Description description) {
  // description.appendText("Expected Cause Error.");
  // }
  //
  // });
  //
  // Throwable cause = new IllegalArgumentException("Cause Test.");
  // throw new RuntimeException(cause);
  // }

}
